using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player_SwordThrowState : PlayerStateBase
{
    private Camera camera;
    private Vector2 direction;
    public Player_SwordThrowState(Player player, StateMachine stateMachine, string animName) : base(player, stateMachine, animName)
    {

    }
    public override void Enter()
    {
        base.Enter();
        if (camera != Camera.main)
            camera = Camera.main;
        skillManager.skillSwordThrow.EnableDots(true);
    }
    public override void Update()
    {
        base.Update();
        direction = GetDirection();

        player.HandleFlip(direction.x);
        
        player.SetVelocity(0, rb.velocity.y);

        skillManager.skillSwordThrow.PredictTrajectory(direction);

        if (input.Player.Attack.WasPressedThisFrame())
        {
            animator.SetBool("SwordThrow_Preform", true);
            skillManager.skillSwordThrow.EnableDots(false);
            skillManager.skillSwordThrow.ConfirmDirection(direction);
        }
           
        if (input.Player.RangeAttack.WasPressedThisFrame() || triggerCalled)
            stateMachine.ChangeState(player.idleState);
    }
    public override void Exit()
    {
        base.Exit();
        animator.SetBool("SwordThrow_Preform", false);
        skillManager.skillSwordThrow.EnableDots(false);
    }
    public Vector2 GetDirection()
    {
        Vector2 mouseWorlsPosition = camera.ScreenToWorldPoint(player.mouseInput);
        Vector2 direction = mouseWorlsPosition - (Vector2)player.transform.position;
        return direction.normalized;
    }
}
